<?php

/**
 * 上传类
 * Date: 2019/03/29
 */

namespace Lib\Service;

use Lib\Base\Request;

class Upload
{
    private static $cos = null;
    private $picLimit;
    private $fileLimit;
    private $error;
    private $actId;

    public function __construct()
    {
        if (self::$cos === null) {
            self::$cos = new Upload2Cos();
        }
        $this->picLimit = 2 * 1024 * 1024; // 最大2M
        $this->fileLimit = 10 * 1024 * 1024; // 最大2M

        if (RUN_ENV == 'internal') {
            $this->actId = Request::param('iAmsActivityId');
        } else {
            $this->actId = Request::param('iActId');
        }
    }

    /**
     * 上传图片
     * @param array $file
     * @return bool|string 成功之后返回图像url，失败返回false
     */
    public function uploadPic($file = [])
    {
        if (empty($file)) {
            $file = Request::fileParam('ulinkPic');
        }

        if (!$file['tmp_name'] || !file_exists($file['tmp_name'])) {
            $this->error = '上传文件不存在';
            return false;
        }
        if (filesize($file['tmp_name']) > $this->picLimit) {
            $this->error = '图片大小不可超过2M';
            return false;
        }
        $type = $file['type'];
        if (substr($type, 0, 6) != 'image/') {
            $this->error = '只支持图片格式上传';
            return false;
        }
        $ext = explode(".", $file['name']);
        $va = count($ext) - 1;
        $suffix = strtolower($ext[$va]);
        if (!in_array($suffix, ['jpg', 'png', 'jpeg', 'gif', 'bmp'])) {
            $this->error = '不支持的后缀名文件(' . $suffix . ')';
            return false;
        }

        if (!$this->actId) {
            $this->error = '需要匹配活动号';
            return false;
        }
        $filename = md5_file($file['tmp_name']) . "." . $suffix;
        $ret = self::$cos->upload($file['tmp_name'], "/$this->actId/$filename");
        if ($ret) {
            if (ENV == 'test') {
                return "https://ulinktest-40095.picsh.qpic.cn/$this->actId/$filename";
            } else {
                return "https://ulink-40088.picsh.qpic.cn/$this->actId/$filename";
            }
        } else {
            $this->error = "[" . self::$cos->err_code . "]" . self::$cos->err_msg;
            recordLog("upload pic error: " . $this->error , __FILE__, __LINE__, LP_ERROR);
            return false;
        }
    }

    /**
     * 上传文件
     * @param array $file
     * @return bool|string
     */
    public function uploadFile($file = [])
    {
        if (empty($file)) {
            $file = Request::fileParam('ulinkFile');
        }
        if (!$file['tmp_name'] || !file_exists($file['tmp_name'])) {
            $this->error = '上传文件不存在';
            return false;
        }
        if (filesize($file['tmp_name']) > $this->fileLimit) {
            $this->error = '文件大小不可超过5M';
            return false;
        }
        $type = $file['type'];
        if (substr($type, 0, 12) != 'application/') {
            $this->error = '只支持文件格式上传';
            return false;
        }
        $ext = explode(".", $file['name']);
        $va = count($ext) - 1;
        $suffix = strtolower($ext[$va]);
        if (!in_array($suffix, ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'zip', 'rar'])) {
            $this->error = '不支持的后缀名文件(' . $suffix . ')';
            return false;
        }

        if (!$this->actId) {
            $this->error = '需要匹配活动号';
            return false;
        }
        $filename = md5_file($file['tmp_name']) . "." . $suffix;
        $ret = self::$cos->upload($file['tmp_name'], "/$this->actId/$filename");
        if ($ret) {
            if (ENV == 'test') {
                return "https://ulinktest-40095.sh.gfp.tencent-cloud.com/$this->actId/$filename";
            } else {
                return "https://ulink-40088.sh.gfp.tencent-cloud.com/$this->actId/$filename";
            }
        } else {
            $this->error = "[" . self::$cos->err_code . "]" . self::$cos->err_msg;
            recordLog("upload file error: " . $this->error , __FILE__, __LINE__, LP_ERROR);
            return false;
        }
    }

    public function getError()
    {
        return $this->error;
    }
}
